You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
946 B
27 lines
946 B
import { oauthManager } from '#server/service/oauth/oauth-manager';
|
|
import { setSessionCookie } from '#server/service/auth/cookie';
|
|
import { OAuthError } from '#server/service/oauth/oauth-error';
|
|
|
|
export default defineWrappedResponseHandler(async (event) => {
|
|
const providerName = getRouterParam(event, 'provider');
|
|
const query = getQuery(event);
|
|
|
|
const { code, state } = query as { code?: string; state?: string };
|
|
|
|
if (!code || !state) {
|
|
return sendRedirect(event, '/auth/login?oauth_error=missing_params');
|
|
}
|
|
|
|
try {
|
|
const result = await oauthManager.handleCallback(providerName!, code, state);
|
|
|
|
if (result.sessionId) {
|
|
setSessionCookie(event, result.sessionId);
|
|
}
|
|
|
|
return sendRedirect(event, '/auth/login?oauth_success=1');
|
|
} catch (error) {
|
|
const errorCode = error instanceof OAuthError ? error.code : 'OAUTH_UNKNOWN';
|
|
return sendRedirect(event, `/auth/login?oauth_error=${errorCode}`);
|
|
}
|
|
});
|